package it.truestoryfactory.tsq.game;

import it.truestoryfactory.tsq.R;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

public class WaitingFragment extends Fragment {
	
	private View myContentView;
	private ProgressBar myProgress;
    private int myProgressStatus = 0;
    private Handler myHandler = new Handler();
    
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		Log.d(this.getClass().getName(), "onActivity created");
		super.onActivityCreated(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		Log.d(this.getClass().getName(), "onCreateView");
		myContentView = inflater.inflate(R.layout.waiting, null);
		
        return myContentView;
        }
	
	@Override
	public void onResume() {
		super.onResume();
		myProgress = (ProgressBar)(getActivity().findViewById(R.id.progress_bar));

        // Start lengthy operation in a background thread
        new Thread(new Runnable() {
        	
        	int counter = 5;

			public void run() {
                while (myProgressStatus < 100) {
                	try {
						Thread.sleep(200);
						if (counter-- == 0){
							myProgressStatus++;
							counter = 5;
						}
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                    // Update the progress bar
                    myHandler.post(new Runnable() {
                        public void run() {
                            myProgress.setProgress(myProgressStatus);
                        }
                    });
                }
            }
        }).start();
	}

	public void setMyProgressStatus(int myProgressStatus) {
		this.myProgressStatus = myProgressStatus;
	}
	
	
}
